import { defineStore } from "pinia";
import { getOptimizedResult } from "./api";
const useResult = defineStore('result', {
    state: () => {
        return {
            data:{
            }
        };
    },
    actions: {
        check(hour){
            return !!this.data[hour]
        },
        async fetchResult(hour){
            console.log(`尝试获取时刻数据${hour}`)
            if(this.check(hour)){
                console.log(`时刻${hour}的数据已存在`)
            }else{
                await getOptimizedResult({time:hour}).then((res)=>{
                    this.data[hour] = res.data
                    console.log(`获取到时刻${hour}的仿真结果`)
                })
            }
        },
        async fetchAllResult(){ 
            for(let i=0;i<24;i++){
                await this.fetchResult(i)
            }
        },
        getDataByName(name){
            const curData = {
                result_optimal:[],
                result_unsleep:[],
                result_without_association:[],
                number:0
            }
            if(JSON.stringify(this.data)==='{}'){
                console.log('数据为空')
                return curData
            }
            for(let i in this.data){
                console.log(`获取${name}的数据`)
                // console.log(curData[result_optimal],this.data[i][result_optimal])
                curData['result_optimal'].push(this.data[i]['result_optimal'][name])
                curData['result_unsleep'].push(this.data[i]['result_unsleep'][name])
                curData['result_without_association'].push(this.data[i]['result_without_association'][name])
                curData.number++
            }
            console.log(`获取${name}的值`,curData)
            return curData
        }
    }
});

export default useResult;